package com.echo.blog.business.blog.warpper;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.echo.blog.business.blog.model.BlogTag;
import com.echo.blog.business.sys.service.ISysUserService;
import com.echo.blog.core.utils.SpringContextHolder;
import com.echo.model.constant.ConstantUtils;
import com.echo.model.utils.ValueUtils;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 博客信息 wrapper
 */
public class BlogWarpper {

    private List<Map<String, Object>> wrapperObj;

    public BlogWarpper(List<Map<String, Object>> obj) {
        this.wrapperObj = obj;
    }

    private ISysUserService userService = SpringContextHolder.getBean(ISysUserService.class);

    /**
     * 包装 wrapperObj
     */
    public List<Map<String, Object>> wrap() {
        //todo 目前暂时使用反显，主要写前端数据   后期引入redis字典缓存
        for (Map<String, Object> map : this.wrapperObj) {
            //createTime 格式化
            if (map.get("createTime") != null) {
                SimpleDateFormat sdf = new SimpleDateFormat(ConstantUtils.Public.DATE_FORMAT_MIN_2);
                String createTime = sdf.format(map.get("createTime"));
                if (createTime != null) {
                    map.put("createTime", createTime);
                }
            }

            if (map.get("blogTagList") != null) {
                //blogTagList 字符串转换成对象
                String blogTagList = ValueUtils.parseString(map.get("blogTagList"));
                List<BlogTag> tags = JSONObject.parseObject(blogTagList, new TypeReference<List<BlogTag>>() {
                });
                map.put("tags", tags);
            }
        }
        return this.wrapperObj;
    }
}
